package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.AppVariable;
import com.example.demo.common.PasswordUtils;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.entity.Userinfo;
import com.example.demo.entity.vo.UserinfoVO;
import com.example.demo.service.ArticleService;
import com.example.demo.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author :蕉绿
 * @Date : 2023/3/14 19:27
 * @Version : V1.0
 * @Description :
 */


//验证前端的数据 正确发给service
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/reg")
    public AjaxResult reg(Userinfo userinfo) {
        //非空校验和参数有效性校验
        if(userinfo == null || !StringUtils.hasLength(userinfo.getUsername())
                            || !StringUtils.hasLength(userinfo.getPassword())) {
            return AjaxResult.fail(-1,"非法参数");
        }
        //密码加密
        userinfo.setPassword(PasswordUtils.encrypt(userinfo.getPassword()));
        return AjaxResult.success(userService.reg(userinfo));
    }

    @RequestMapping("/login")
    public AjaxResult login(HttpServletRequest request,String username, String password) {
        //非空校验和参数有效性校验
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return AjaxResult.fail(-1,"非法参数");
        }
        Userinfo userinfo = userService.getUserByName(username);
        if(userinfo!=null || userinfo.getId() > 0) {
            //
            if(PasswordUtils.check(password,userinfo.getPassword())) {
                //登录成功

                //将用户存储到session
                HttpSession session = request.getSession();
                session.setAttribute(AppVariable.USER_SESSION_KEY,userinfo);
                userinfo.setPassword(""); //隐藏敏感信息

                return AjaxResult.success(userinfo);
            }
        }
        return AjaxResult.success(0,null);
    }

    @RequestMapping("/showinfo")
    public AjaxResult showInfo(HttpServletRequest request) {
        UserinfoVO userinfoVO = new UserinfoVO();
        //1.得到当前登录的用户(从session中
        Userinfo userinfo = UserSessionUtils.getSessionUser(request);
        if(userinfo == null) {
            return AjaxResult.fail(-1,"非法请求!");
        }
        //spring 深克隆方法
        BeanUtils.copyProperties(userinfo,userinfoVO);
        //2.得到用户发表的文章总数
        userinfoVO.setArtCount(articleService.getArtCountByUid(userinfoVO.getId()));
        return AjaxResult.success(userinfoVO);
    }
    @RequestMapping("/logout")
    public AjaxResult logout(HttpSession session) {
        session.removeAttribute(AppVariable.USER_SESSION_KEY);
        return AjaxResult.success(1);
    }

    @RequestMapping("/getuserbyid")
    public AjaxResult getUserById(Integer id) {
        if(id == null || id <= 0) {
           //无效参数
            return AjaxResult.fail(-1,"非法参数");
        }
        Userinfo userinfo = userService.getUserById(id);
        if(userinfo == null || userinfo.getId() <= 0) {
            //无效参数
            return AjaxResult.fail(-1,"非法参数");

        }
        //去除敏感数据
        userinfo.setPassword("");
        //查询当前用户发表文章数
        UserinfoVO userinfoVO = new UserinfoVO();
        BeanUtils.copyProperties(userinfo,userinfoVO);
        userinfoVO.setArtCount(articleService.getArtCountByUid(id));
        return AjaxResult.success(userinfoVO);
    }

}
